//
//  MagicMovePushedViewController.m
//  AnimationDemo
//
//  Created by jiaozenglian on 2020/7/13.
//  Copyright © 2020 Jiaozl. All rights reserved.
//

#import "MagicMovePushedViewController.h"
#import "ZLTransitionDelegate.h"
#import "ZLInteractiveTransition.h"
#import "MagicMoveAnimatedTransition.h"
@interface MagicMovePushedViewController ()

@property (nonatomic, strong) UIScreenEdgePanGestureRecognizer* gestureRecognizer;
@property (nonatomic, strong) UIImageView *imageView;
@property (nonatomic, strong) ZLTransitionDelegate *transitionDelegate;
@property (nonatomic, strong) MagicMoveAnimatedTransition *moveAnimatedTransition;

@end

@implementation MagicMovePushedViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"zrx4.jpg"]];
    [self.view addSubview:imageView];
    imageView.frame = CGRectMake(10, 10, mScreenWidth - 20, mScreenWidth - 20);
    self.imageView = imageView;
    
    UITextView *textView = [[UITextView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(self.imageView.frame), mScreenWidth, mScreenHeight - CGRectGetMaxY(self.imageView.frame))];
    textView.text = @"这是类似于KeyNote的神奇移动效果，向右滑动可以通过手势控制POP动画";
    textView.font = [UIFont systemFontOfSize:14];
    [self.view addSubview:textView];
    [self setupGesture];
}

- (void)setupGesture
{
    // 添加屏幕边缘滑动手势
    UIPanGestureRecognizer *screenEdgePanGR = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(_actionLeftSwipe:)];
    [self.view addGestureRecognizer:screenEdgePanGR];
}

- (void)_actionLeftSwipe:(UIScreenEdgePanGestureRecognizer *)sender
{
    [self _actionPresentNewVC:sender];
}

- (void)_actionPresentNewVC:(id)sender
{
    if ([sender isKindOfClass:UIGestureRecognizer.class] ) {
        if (((UIGestureRecognizer *)sender).state == UIGestureRecognizerStateBegan) {
            self.gestureRecognizer = sender;
            [self.navigationController popViewControllerAnimated:YES];
        }
    }
}

- (BOOL)navigationShouldPopOnBackButton
{
    self.gestureRecognizer = nil;
    self.moveAnimatedTransition.transitionType = ZLTransitionTypePop;
    return YES;
}

// MARK: - getter
- (ZLTransitionDelegate *)transitionDelegate
{
    if (!_transitionDelegate) {
        _transitionDelegate = [[ZLTransitionDelegate alloc] init];
        weakify(self);
        _transitionDelegate.animatedPresentTransition = ^id<UIViewControllerAnimatedTransitioning> _Nonnull{
            strongify(self);
            self.moveAnimatedTransition.transitionType = ZLTransitionTypePush;
            return self.moveAnimatedTransition;
        };
        _transitionDelegate.animatedDismissalTransition = ^id<UIViewControllerAnimatedTransitioning> _Nonnull{
            strongify(self);
            self.moveAnimatedTransition.transitionType = ZLTransitionTypePop;
            return self.moveAnimatedTransition;
        };
        _transitionDelegate.interactivePresentTransition = ^id<UIViewControllerInteractiveTransitioning> _Nonnull{
            strongify(self);
            if (self.gestureRecognizer) {
                return [[ZLInteractiveTransition alloc] initWithGestureRecognizer:self.gestureRecognizer edgeForDragging:UIRectEdgeRight];
            }
            return nil;
        };
        _transitionDelegate.interactiveDismissalTransition = ^id<UIViewControllerInteractiveTransitioning> _Nonnull{
            strongify(self);
            if (self.gestureRecognizer) {
                return [[ZLInteractiveTransition alloc] initWithGestureRecognizer:self.gestureRecognizer edgeForDragging:UIRectEdgeLeft];
            }
            return nil;
        };
    }
    return _transitionDelegate;
}

- (MagicMoveAnimatedTransition *)moveAnimatedTransition
{
    if (!_moveAnimatedTransition) {
        _moveAnimatedTransition = [MagicMoveAnimatedTransition animatedTransition];
    }
    return _moveAnimatedTransition;
}


@end
